use crate::pool;
use rbatis::{crud, impl_delete, impl_select_page, Page, rbdc::{self, db::ExecResult, Error}, sql::{self, PageRequest}};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TbApp {
    pub id: Option<String>,
    pub appid: Option<String>,
    pub authid: Option<String>,
    pub authpwd: Option<String>,
    pub token_cnt: Option<i32>,
    pub token_ts: Option<u32>,
    pub expire_ts: Option<i64>,
    pub desc: Option<String>,
    pub expire_day: Option<String>,
}

crud!(TbApp {});
impl_select_page!(TbApp {select_page() => ""});
impl_delete!(TbApp {delete(appid: &String, authid: &String) => "`where appid=#{appid} and authid=#{authid}`"});

impl TbApp {
    pub async fn get_apps(
        page: &crate::common::QueryPage,
    ) -> Result<Page<TbApp>, rbdc::Error> {
        TbApp::select_page(pool!(), &PageRequest::from(page)).await
    }

    pub async fn delete_app(appid: &String, authid: &String) -> Result<ExecResult, Error> {
        TbApp::delete(pool!(), appid, authid).await
    }
}
